package com.example.demo.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.*;
import com.example.demo.mapper.*;
import com.example.demo.security.util.JwtUtils;
import com.example.demo.service.BusinessService;
import com.tencentcloudapi.cynosdb.v20190107.models.Addr;
import io.swagger.models.auth.In;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.*;

@Service
public class BusinessServiceImpl implements BusinessService {
    @Resource
    private BusinessMapper businessMapper;

    @Resource
    private AuthenticationManager authenticationManager;
    @Resource
    private AwardMapper awardMapper;
    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private AddressMapper addressMapper;
    @Resource
    private EmployeeRoleMapper employeeRoleMapper;

    @Resource
    private ProgressMapper progressMapper;


    public int register(Business business) {
        QueryWrapper<Business> wrapper = new QueryWrapper<Business>();
        wrapper.eq("business_phone", business.getBusinessPhone());
        PasswordEncoder encoder=new BCryptPasswordEncoder();
        business.setBusinessPassword(encoder.encode(business.getBusinessPassword()));
        List<Business> res = businessMapper.selectList(wrapper);
        if (res != null && !res.isEmpty()) {
            return 0;
        } else {
            return businessMapper.insert(business);
        }
    }


    public Business login(Business business) {
        QueryWrapper<Business> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("business_phone",business.getBusinessPhone());
        queryWrapper.eq("business_password",business.getBusinessPassword());
        List<Business> res=businessMapper.selectList(queryWrapper);

        //如果认证没通过
        if(res!=null&&res.size()==0){
            return null;
        }
        return res.get(0);
    }

    public int reset(String businessPhone,String newPassword) {
        UpdateWrapper<Business> updateWrapper = new UpdateWrapper<Business>();
        updateWrapper.eq("business_phone",businessPhone);
        updateWrapper.set("business_password",newPassword);
        return businessMapper.update(null,updateWrapper);
    }

    public int editBusiness(Business business){
        QueryWrapper<Business> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id",business.getBusinessId());
        List<Business> res = businessMapper.selectList(queryWrapper);
        if(res != null){
            UpdateWrapper<Business> updateWrapper=new UpdateWrapper<>();
            updateWrapper.eq("business_id",business.getBusinessId());
            businessMapper.update(business,updateWrapper);
            return 1;
        }
        return 0;
    }
    @Override
    public Business getBusinessById(int businessId){
        Business res=businessMapper.selectById(businessId);
        if(res != null){
            return res;
        }else{
            return null;
        }
    }

    @Override

    public List<Business> getBusinessList(){
       return businessMapper.selectList(null);
    }

    @Override
    public int delBusiness(MyArray ids){
        List<Integer> list=new ArrayList<>();
        for(int id:ids.getIds()){
            list.add(id);
        }
        try{
            return businessMapper.deleteBatchIds(list);
        }catch (Exception e){
            return 0;
        }

    }

    @Resource
    private ActivityMapper activityMapper;
    public List<BusinessOrder> getOrderList(Integer businessId){
        QueryWrapper<UserAward> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id",businessId);
        List<UserAward> res = awardMapper.selectList(queryWrapper);
        List<BusinessOrder> businessOrders = new ArrayList<>();
        for(UserAward r : res){
            Goods goods = goodsMapper.selectById(r.getGoodId());
            UserAddress userAddress = addressMapper.selectById(r.getUserAddressId());
            Activity activity = activityMapper.selectById(r.getActivityId());

            BusinessOrder businessOrder = new BusinessOrder(r.getOrderId(), goods, userAddress, activity, r.getShipStatus(), r.getOrderDate());
            businessOrders.add(businessOrder);
        }
        return businessOrders;
    }


//    public List<BusinessOrder> getPrizeListShipped(Integer businessId){
//        QueryWrapper<UserAward> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("business_id",businessId);
//        queryWrapper.eq("ship_status","shipped");
//        List<UserAward> res = awardMapper.selectList(queryWrapper);
//        List<BusinessOrder> businessOrders = new ArrayList<>();
//        for(UserAward r : res){
//            QueryWrapper<Goods> queryWrapper1 = new QueryWrapper<>();
//            queryWrapper1.eq("good_id",r.getGoodId());
//            Goods goods = goodsMapper.selectOne(queryWrapper1);
//            QueryWrapper<UserAddress> queryWrapper2 = new QueryWrapper<>();
//            queryWrapper2.eq("user_address_id",r.getUserAddressId());
//            UserAddress userAddress = addressMapper.selectOne(queryWrapper2);
//            BusinessOrder businessOrder = new BusinessOrder(r.getOrderId(),goods,userAddress,r.getOrderDate());
//            businessOrders.add(businessOrder);
//        }
//        return businessOrders;
//    }
    public Integer ship(String orderId, String sendId, String company, String remarks){
        UpdateWrapper<UserAward> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("order_id",orderId);
        UserAward userAward = new UserAward();
        userAward.setShipStatus("1");
        userAward.setSendId(sendId);
        userAward.setCompany(company);
        userAward.setRemarks(remarks);
        return awardMapper.update(userAward,updateWrapper);
    }
    @Override
    public Integer delEmployee(MyArray ids){
        List<Integer> list=new ArrayList<>();
        for(int id:ids.getIds()){
            list.add(id);
        }
        QueryWrapper<EmployeeRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.in("employee_id",list);
        try{
            employeeRoleMapper.delete(queryWrapper);
            return employeeMapper.deleteBatchIds(list);
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public Integer editEmployeeMsg(Employee employee){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("employee_id",employee.getEmployeeId());
        return employeeMapper.update(employee,updateWrapper);
    }

    @Override
    public List<Progress> getProgressByOrderId(String orderId) {
        QueryWrapper<Progress> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId);
        List<Progress> res=progressMapper.selectList(queryWrapper);
        return res;
    }

    @Override
    public List<Map<String, Object>> getStatistics(Integer businessId) {
        return awardMapper.getStatistics(businessId);
    }


    @Override
    public LogisticsMsg getLogisticsMsgByOrderId(String orderId){
        QueryWrapper<UserAward> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId);
        UserAward userAward = awardMapper.selectOne(queryWrapper);
        LogisticsMsg logisticsMsg = new LogisticsMsg(userAward.getSendId(),userAward.getCompany(),userAward.getRemarks());
        return logisticsMsg;
    }
}